package com.pss.order.controller;

import com.github.pagehelper.PageInfo;
import com.pss.common.component.LoginUserHolder;
import com.pss.common.response.Response;
import com.pss.order.entity.BOrder;
import com.pss.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/order")
@Api(value = "/order", description = "订单", tags = "订单接口")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private LoginUserHolder loginUserHolder;

    @GetMapping("/")
    public String helloWorld(){
        return "helloWorld";
    }

    @ApiOperation(value = "提交订单", httpMethod = "POST", notes = "提交订单")
    @PostMapping("/submitOrder")
    public Response<BOrder> submitOrder(@RequestParam("pId") Integer pId){
        log.info("提交订单{}", pId);
        BOrder s = orderService.submitOrder(pId);
        return Response.success(s);
    }

    @ApiOperation(value = "提交订单", httpMethod = "POST", notes = "提交订单")
    @PostMapping("/submitOrder2")
    public Response<BOrder> submitOrder2(@Validated @RequestBody BOrder bOrder){
        orderService.sendMq();
        return Response.success(bOrder);
    }

    @ApiOperation(value = "提交订单分库分表插入", httpMethod = "POST", notes = "提交订单分库分表插入")
    @PostMapping("/submitOrder3")
    public Response<List<BOrder>> submitOrder3(@Validated @RequestBody List<BOrder> bOrders){
        orderService.submitOrder3(bOrders);
        return Response.success(bOrders);
    }

    @ApiOperation(value = "提交订单分库分表查询", httpMethod = "POST", notes = "提交订单分库分表查询")
    @PostMapping("/selectAll")
    public Response<PageInfo<BOrder>> selectAll(int page, int size){
//        Map currentUser = loginUserHolder.getCurrentUser();
        return Response.success(orderService.selectAll(page, size));
    }

}
